package com.example.zqm.git72.Adapter;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.zqm.git72.Activity.ClubDetailActivity;
import com.example.zqm.git72.Activity.TrainDetailActivity;
import com.example.zqm.git72.JavaBean.Collection;
import com.example.zqm.git72.JavaBean.Collectiontrain;
import com.example.zqm.git72.Model.ClubModel;
import com.example.zqm.git72.Model.TrainModel;
import com.example.zqm.git72.R;
import com.squareup.picasso.Picasso;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

//import com.example.zqm.git72.Activity.FoodActivity;

/**
 * Created by Administrator on 2017/3/6 0006.
 */

public class CollectiontrainAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{

    private Context mContext;
    private List<Collectiontrain> mDataList;
    private LayoutInflater mLayoutInflater;
    String user_id;
    String train_id;
    //构造方法
    public CollectiontrainAdapter(Context mContext, List<Collectiontrain> mDataList) {
        this.mContext = mContext;
        this.mDataList = mDataList;
        mLayoutInflater = LayoutInflater.from(mContext);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = mLayoutInflater.from(parent.getContext()).inflate(R.layout.cardview_collect,parent,false);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        //把viewhoder数据映射到
        final Collectiontrain entity = mDataList.get(position);
        if (null == entity)
            return;
        ViewHolder viewHolder = (ViewHolder) holder;
        viewHolder.collect_name.setText(entity. getTrain_id());
        viewHolder.collect_content.setText("❤"+entity.getInsert_time());
//        if(entity.getThumb().equals("")){
//            Picasso.with(mContext).load("https://b-ssl.duitang.com/uploads/item/201409/03/20140903102455_SNNnA.png"
//            )
//                    .resize(50,50).into(viewHolder.collect_pic);
//        }else{
//            String picUrl = "http://172.24.10.175/workout/Uploads/" + entity.getThumb();
//            Picasso.with(mContext).load(picUrl.toString())
//                    .resize(50,50).into(viewHolder.collect_pic);
//        }

        //Context上下文在很多Android Api中都是必须的,图片网络加载地址,想进行图片展示的ImageView
        viewHolder.btn_enter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent();
                intent.putExtra("id",entity.getTrain_id());
                intent.setClass(mContext, TrainDetailActivity.class);
                mContext.startActivity(intent);
            }
        });
        viewHolder.btn_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SharedPreferences sharedPreferences=mContext.getSharedPreferences("userInfo", Context.MODE_PRIVATE);
                user_id=sharedPreferences.getString("user_id","");
                train_id=entity.getTrain_id();
                TrainModel trainModel =new TrainModel();
                Call<Integer> call =trainModel.decollect(train_id,user_id);
                call.enqueue(new Callback<Integer>() {
                    @Override
                    public void onResponse(Call<Integer> call, Response<Integer> response) {
                        Toast.makeText(mContext,"取消关注成功",Toast.LENGTH_SHORT).show();
                        //if (response.body().toString().trim().equals("0")){}
                    }
                    @Override
                    public void onFailure(Call<Integer> call, Throwable t) {
                        Toast.makeText(mContext,"取消关注失败",Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });

    }

    @Override
    public int getItemCount() {
        return mDataList.size();
    }


    public class ViewHolder extends RecyclerView.ViewHolder{
        ImageView collect_pic;
        TextView collect_name,collect_content;
        Button btn_cancel,btn_enter;
        //构造方法
        public ViewHolder(View itemView) {
            super(itemView);
            collect_name = (TextView) itemView.findViewById(R.id.collect_name);
            collect_pic = (ImageView) itemView.findViewById(R.id.collect_pic);
            collect_content=(TextView) itemView.findViewById(R.id.collect_content);
            btn_cancel =(Button)itemView.findViewById(R.id.btn_cancel);
            btn_enter=(Button)itemView.findViewById(R.id.btn_enter);
        }
    }

}
